package com.cmc.cloud.cmclink.doc.service.impl;

import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import com.cmc.cloud.cmclink.doc.constants.errorconstant.ArrivalnoticePrintSetupErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.convert.ArrivalnoticePrintSetupConvert;
import com.cmc.cloud.cmclink.doc.entity.ArrivalnoticePrintSetupDO;
import com.cmc.cloud.cmclink.doc.mapper.ArrivalnoticePrintSetupMapper;
import com.cmc.cloud.cmclink.doc.service.ArrivalnoticePrintSetupService;
import com.cmc.cloud.cmclink.doc.vo.arrivalnoticeprintsetupvo.ArrivalnoticePrintSetupBaseVO;
import com.cmc.cloud.cmclink.doc.vo.arrivalnoticeprintsetupvo.ArrivalnoticePrintSetupCreateReqVO;
import com.cmc.cloud.cmclink.doc.vo.arrivalnoticeprintsetupvo.ArrivalnoticePrintSetupPageReqVO;
import com.cmc.cloud.cmclink.doc.vo.arrivalnoticeprintsetupvo.ArrivalnoticePrintSetupUpdateReqVO;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;

import cn.hutool.core.collection.CollectionUtil;

/**
 * Arrival Notice 打印配置 Service 实现类
 *
 * @author 集运管理员
 */
@Service
@Validated
public class ArrivalnoticePrintSetupServiceImpl implements ArrivalnoticePrintSetupService {

    @Resource
    private ArrivalnoticePrintSetupMapper arrivalnoticePrintSetupMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createArrivalnoticePrintSetup(ArrivalnoticePrintSetupCreateReqVO createReqVO) {
        // 插入
        validateUniqueExists(createReqVO, null);
        ArrivalnoticePrintSetupDO arrivalnoticePrintSetup =
            ArrivalnoticePrintSetupConvert.INSTANCE.convert(createReqVO);
        arrivalnoticePrintSetupMapper.insert(arrivalnoticePrintSetup);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateArrivalnoticePrintSetup(ArrivalnoticePrintSetupUpdateReqVO updateReqVO) {
        // 校验存在
        validateArrivalnoticePrintSetupExists(updateReqVO.getId());
        validateUniqueExists(updateReqVO, updateReqVO.getId());
        // 更新
        ArrivalnoticePrintSetupDO updateObj = ArrivalnoticePrintSetupConvert.INSTANCE.convert(updateReqVO);
        arrivalnoticePrintSetupMapper.updateById(updateObj);
    }

    @Override
    public void deleteArrivalnoticePrintSetup(Long id) {
        // 校验存在
        validateArrivalnoticePrintSetupExists(id);
        // 删除
        arrivalnoticePrintSetupMapper.deleteById(id);
    }

    private void validateArrivalnoticePrintSetupExists(Long id) {
        if (arrivalnoticePrintSetupMapper.selectById(id) == null) {
            throw ServiceExceptionUtil
                .exception(ArrivalnoticePrintSetupErrorCodeConstants.ARRIVALNOTICE_PRINT_SETUP_NOT_EXISTS);
        }
    }

    private void validateUniqueExists(ArrivalnoticePrintSetupBaseVO vo, Long id) {
        LambdaQueryWrapperX<ArrivalnoticePrintSetupDO> queryWrapperX =
            new LambdaQueryWrapperX<ArrivalnoticePrintSetupDO>().eqIfPresent(ArrivalnoticePrintSetupDO::getPortCode,
                vo.getPortCode());

        if (id != null) {
            queryWrapperX.ne(ArrivalnoticePrintSetupDO::getId, id);
        }
        if (arrivalnoticePrintSetupMapper.selectCount(queryWrapperX) != 0L) {
            throw ServiceExceptionUtil
                .exception(ArrivalnoticePrintSetupErrorCodeConstants.ARRIVALNOTICE_PRINT_SETUP_PORT_EXISTS);
        }
    }

    @Override
    public ArrivalnoticePrintSetupDO getArrivalnoticePrintSetup(Long id) {
        return arrivalnoticePrintSetupMapper.selectById(id);
    }

    @Override
    public List<ArrivalnoticePrintSetupDO> getArrivalnoticePrintSetupList(Collection<Long> ids) {
        return arrivalnoticePrintSetupMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ArrivalnoticePrintSetupDO>
        getArrivalnoticePrintSetupPage(ArrivalnoticePrintSetupPageReqVO reqVO) {
        LambdaQueryWrapperX<ArrivalnoticePrintSetupDO> queryWrapperX =
            new LambdaQueryWrapperX<ArrivalnoticePrintSetupDO>().likeIfPresent(ArrivalnoticePrintSetupDO::getPortCode,
                reqVO.getPortCode());
        return arrivalnoticePrintSetupMapper.selectPage(reqVO, queryWrapperX);
    }

    @Override
    public ArrivalnoticePrintSetupDO getArrivalnoticePrintSetupByPortCode(String portCode) {
        LambdaQueryWrapperX<ArrivalnoticePrintSetupDO> queryWrapperX = new LambdaQueryWrapperX<>();
        queryWrapperX.eq(ArrivalnoticePrintSetupDO::getPortCode, portCode);
        List<ArrivalnoticePrintSetupDO> arrivalList = arrivalnoticePrintSetupMapper.selectList(queryWrapperX);
        if (!CollectionUtil.isEmpty(arrivalList)) {
            return arrivalList.get(0);
        }
        return new ArrivalnoticePrintSetupDO();
    }
}
